from socketio import Namespace
from utils.unmashal_with_class import unmashal_witch_class
from messages.TextMessage import TextMessage
from messages.FileMessage import FileMessage


class BaseNamespace(Namespace):

    @unmashal_witch_class(TextMessage)
    def on_send_text(self, sid, data: TextMessage):
        data.load_user()
        self.emit('recv', data.to_dict(), room=data.to['username'])

    @unmashal_witch_class(FileMessage)
    def on_send_file(self, sid, data: FileMessage):
        data.load_user()
        data.upload()
        self.emit('recv', data.to_dict(), room=data.to['username'])
